﻿package org.xhs.report.doc.ui
{
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.*;
    import baidu.wenku.common.ui.panel.*;
    import baidu.wenku.common.ui.toolbar.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.doc.*;
    import baidu.wenku.doc.model.*;
    import flash.display.*;
    import flash.events.*;
    import flash.external.*;

    public class ToolBar extends Sprite
    {
        protected var _pageth:int = 0;
        protected var _pagesAllAvailable:int = 0;
        protected var bmFirstTime:Boolean = true;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var relativeModel:RelativeModel;
        protected var screenModel:ScreenModel;
        protected var seriesModel:SeriesModel;
        protected var seriesPanel:SeriesPanel;
        protected var toolbarBg:ToolbarBG;
        protected var bookmarkBar:BookmarkBar;
        protected var toolButtonLayout:ToolButtonLayout;
        protected var searchPanel:SearchPanel;
        protected var zoomModel:DocZoomModel;
        protected var locatModel:DocLocatModel;
        protected var pageGroup:PageGroup;

        public function ToolBar()
        {
            this.bmFirstTime = true;
            if (Config.isAPIViewer)
            {
                WenkuLog.addLog(WenkuLog.LOGURL_LOADSUCCESS_API, "", 2);
            }
            else
            {
                WenkuLog.addLog(WenkuLog.LOGURL_LOADSUCCESS_VIEW);
            }
            this.initPanels();
            this.initInterface();
            return;
        }// end function

        protected function initPanels() : void
        {
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.relativeModel = InstanceManager.createSingletonInstance(RelativeModel);
            this.seriesModel = InstanceManager.createSingletonInstance(SeriesModel);
            this.seriesPanel = InstanceManager.createSingletonInstance(SeriesPanel);
            this.zoomModel = InstanceManager.createSingletonInstance(DocZoomModel);
            this.zoomModel.addEventListener(DocZoomModel.ZOOM_CHANGED, this.zoomChanged);
            this.locatModel = InstanceManager.createSingletonInstance(DocLocatModel);
            this.locatModel.addEventListener(LocatEvent.PAGE_LOADED, this.pageLoaded);
            this.locatModel.addEventListener(LocatEvent.PAGETH_CHANGED_PAGEGROUP, this.pagethChanged);
            this.locatModel.addEventListener(LocatEvent.PROCESS_NO_DOC, this.disableAll);
            this.locatModel.addEventListener(LocatEvent.PROCESS_DEL_DOC, this.disableAll);
            this.searchPanel = new SearchPanel(this.locatModel);
            this.searchPanel.addEventListener(ToolbarEvent.SEARCH_START, this.searchHandler);
            this.searchPanel.addEventListener(ToolbarEvent.SEARCH_AUTO, this.searchHandler);
            this.searchPanel.addEventListener(ToolbarEvent.SEARCH_PREV, this.searchHandler);
            this.searchPanel.addEventListener(ToolbarEvent.SEARCH_NEXT, this.searchHandler);
            this.searchPanel.addEventListener(ToolbarEvent.SEARCH_CLOSE, this.searchHandler);
            addChild(this.searchPanel);
            this.bookmarkBar = InstanceManager.createSingletonInstance(BookmarkBar);
            this.bookmarkBar.addEventListener(BookmarkBar.BOOKMARK_CLICK, this.bookmarkClick);
            this.toolButtonLayout = InstanceManager.createSingletonInstance(ToolButtonLayout);
            this.toolButtonLayout.addEventListener(ToolButtonEvent.SCREEN_FULL, this.fullBtnClick);
            this.toolButtonLayout.addEventListener(ToolButtonEvent.SCREEN_NORMAL, this.normalBtnClick);
            this.toolButtonLayout.addEventListener(ToolButtonEvent.MODE_SINGLE, this.singleBtnClick);
            this.toolButtonLayout.addEventListener(ToolButtonEvent.MODE_DOUBLE, this.doubleBtnClick);
            this.toolButtonLayout.addEventListener(ToolButtonEvent.ZOOM_IN, this.zoominBtnClick);
            this.toolButtonLayout.addEventListener(ToolButtonEvent.ZOOM_OUT, this.zoomoutBtnClick);
            addChild(this.toolButtonLayout);
            return;
        }// end function

        protected function initInterface() : void
        {
            try
            {
                ExternalInterface.addCallback(JSInterface.JS_NEWMARK, this.showNewMarkInfo);
            }
            catch (e:Error)
            {
                return;
            }
            return;
        }// end function

        protected function pageLoaded(event:LocatEvent) : void
        {
            this.resetPageModeBtns();
            this.pagesAllAvailable = this.locatModel.pagesAllAvailable;
            this.pageGroup.visible = true;
            this.pageth = this.locatModel.pageth;
            return;
        }// end function

        protected function pagethChanged(event:LocatEvent) : void
        {
            this.pageth = this.locatModel.pageth;
            return;
        }// end function

        protected function searchHandler(event:ToolbarEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        protected function addBG() : void
        {
            this.toolbarBg = new ToolbarBG();
            addChildAt(this.toolbarBg, 0);
            return;
        }// end function

        protected function addPageGroup() : void
        {
            this.pageGroup = new PageGroup(this.locatModel);
            this.pageGroup.addEventListener(PageGroup.PREV_CLICK, this.prevBtnClick);
            this.pageGroup.addEventListener(PageGroup.NEXT_CLICK, this.nextBtnClick);
            addChild(this.pageGroup);
            return;
        }// end function

        protected function addSearchGroup() : void
        {
            addChild(this.searchPanel);
            return;
        }// end function

        protected function addBookMarkBtn() : void
        {
            this.bookmarkBar.show();
            addChild(this.bookmarkBar);
            return;
        }// end function

        protected function bookmarkClick(event:Event) : void
        {
            if (contains(this.bookmarkBar))
            {
                removeChild(this.bookmarkBar);
            }
            if (this.loaderInfoVo.bookmarkPage > 0 && this.loaderInfoVo.bookmarkPage <= this._pagesAllAvailable)
            {
                this.pageth = 0;
                dispatchEvent(new ToolbarEvent(ToolbarEvent.PAGETH_CHANGED));
            }
            return;
        }// end function

        protected function showNewMarkInfo(param1:String, param2:int = 0) : void
        {
            this.bookmarkBar.showNewMarkInfo(param1, param2);
            addChild(this.bookmarkBar);
            if (param2)
            {
                this.loaderInfoVo.newmarkPage = this._pageth + 1;
                this.toolButtonLayout.disableBtnByName("newmarkBtn");
            }
            return;
        }// end function

        protected function zoomChanged(event:Event) : void
        {
            this.resetZoomLevel();
            return;
        }// end function

        protected function fullBtnClick(event:ToolButtonEvent = null) : void
        {
            dispatchEvent(new ToolbarEvent(ToolbarEvent.SCREEN_FULL));
            WenkuLog.addLog(WenkuLog.LOGURL_FULLSCREEN);
            return;
        }// end function

        protected function normalBtnClick(event:ToolButtonEvent = null) : void
        {
            dispatchEvent(new ToolbarEvent(ToolbarEvent.SCREEN_NORMAL));
            return;
        }// end function

        protected function prevBtnClick(event:Event = null) : void
        {
            if (this.locatModel.pageth < 1)
            {
                return;
            }
            this.locatModel.pageth = this.locatModel.pageth - (DocConfig.pageMode == DocConfig.PAGE_MODE_DOUBLE ? (2) : (1));
            WenkuLog.addLog(WenkuLog.LOGURL_PAGEUP + String((this._pageth + 1)));
            return;
        }// end function

        protected function nextBtnClick(event:Event = null) : void
        {
            if (this.locatModel.pageth > this.locatModel.pagesAllAvailable - 2)
            {
                return;
            }
            this.locatModel.pageth = this.locatModel.pageth + (DocConfig.pageMode == DocConfig.PAGE_MODE_DOUBLE ? (2) : (1));
            WenkuLog.addLog(WenkuLog.LOGURL_PAGEDOWN + String((this.locatModel.pageth + 1)));
            return;
        }// end function

        protected function singleBtnClick(event:ToolButtonEvent = null) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_SINGLEPAGE + "&pn=" + (this._pageth + 1));
            this.toolButtonLayout.disableBtnByName("singleBtn");
            this.toolButtonLayout.enableBtnByName("doubleBtn");
            dispatchEvent(new ToolbarEvent(ToolbarEvent.MODE_SINGLE));
            return;
        }// end function

        protected function doubleBtnClick(event:ToolButtonEvent = null) : void
        {
            WenkuLog.addLog(WenkuLog.LOGURL_DOUBLEPAGE + "&pn=" + (this._pageth + 1));
            this.toolButtonLayout.disableBtnByName("doubleBtn");
            this.toolButtonLayout.enableBtnByName("singleBtn");
            dispatchEvent(new ToolbarEvent(ToolbarEvent.MODE_DOUBLE));
            return;
        }// end function

        protected function zoominBtnClick(event:ToolButtonEvent = null) : void
        {
            if (this.zoomModel.zoomLevel < (this.zoomModel.zoomLevelNum - 1))
            {
                var _loc_2:* = this.zoomModel;
                var _loc_3:* = this.zoomModel.zoomLevel + 1;
                _loc_2.zoomLevel = _loc_3;
                WenkuLog.addLog(WenkuLog.LOGURL_ZOOMIN, "", 3);
            }
            return;
        }// end function

        protected function zoomoutBtnClick(event:ToolButtonEvent = null) : void
        {
            if (this.zoomModel.zoomLevel > 0)
            {
                var _loc_2:* = this.zoomModel;
                var _loc_3:* = this.zoomModel.zoomLevel - 1;
                _loc_2.zoomLevel = _loc_3;
                WenkuLog.addLog(WenkuLog.LOGURL_ZOOMOUT, "", 3);
            }
            return;
        }// end function

        protected function resetZoomLevel() : void
        {
            if (this._pagesAllAvailable <= 0)
            {
                return;
            }
            if (this.zoomModel.zoomLevel > 0)
            {
                this.toolButtonLayout.enableBtnByName("zoomoutBtn");
            }
            else
            {
                this.toolButtonLayout.disableBtnByName("zoomoutBtn");
            }
            if (this.zoomModel.zoomLevel < (this.zoomModel.zoomLevelNum - 1))
            {
                this.toolButtonLayout.enableBtnByName("zoominBtn");
            }
            else
            {
                this.toolButtonLayout.disableBtnByName("zoominBtn");
            }
            return;
        }// end function

        protected function disableAll(event:LocatEvent = null) : void
        {
            this.pageGroup.visible = true;
            this.pageGroup.disableAll();
            this.toolButtonLayout.disableBtnByName("zoominBtn");
            this.toolButtonLayout.disableBtnByName("zoomoutBtn");
            this.toolButtonLayout.disableBtnByName("singleBtn");
            this.toolButtonLayout.disableBtnByName("doubleBtn");
            this.toolButtonLayout.disableBtnByName("seriesBtn");
            if (Config.isAPIViewer)
            {
                this.toolButtonLayout.disableBtnByName("downloadBtn");
                this.toolButtonLayout.disableBtnByName("shareBtn");
                this.toolButtonLayout.disableBtnByName("relativeBtn");
            }
            else
            {
                this.searchPanel.enabled = false;
            }
            this.toolButtonLayout.disableBtnByName("fullBtn");
            this.toolButtonLayout.disableBtnByName("normalBtn");
            this.toolButtonLayout.disableBtnByName("newmarkBtn");
            return;
        }// end function

        protected function set pagesAllAvailable(param1:int) : void
        {
            if (param1 <= 0)
            {
                this.disableAll();
                return;
            }
            this._pagesAllAvailable = param1;
            this.toolButtonLayout.enableBtnByName("downloadBtn");
            this.toolButtonLayout.enableBtnByName("shareBtn");
            this.toolButtonLayout.enableBtnByName("relativeBtn");
            this.toolButtonLayout.enableBtnByName("fullBtn");
            this.toolButtonLayout.enableBtnByName("normalBtn");
            this.resetZoomLevel();
            this.searchPanel.enabled = true;
            this.seriesModel.loadData();
            return;
        }// end function

        protected function get pagesAllAvailable() : int
        {
            return this._pagesAllAvailable;
        }// end function

        public function init() : void
        {
            this.addBG();
            this.addPageGroup();
            this.addSearchGroup();
            return;
        }// end function

        public function resetPageModeBtns() : void
        {
            if (DocConfig.pageMode == DocConfig.PAGE_MODE_SINGLE)
            {
                this.toolButtonLayout.disableBtnByName("singleBtn");
                this.toolButtonLayout.enableBtnByName("doubleBtn");
            }
            else
            {
                this.toolButtonLayout.enableBtnByName("singleBtn");
                this.toolButtonLayout.disableBtnByName("doubleBtn");
            }
            return;
        }// end function

        public function doMouseWheel(param1:Number) : void
        {
            var delta:* = param1;
            try
            {
                delta = delta * 24;
                if (this.seriesPanel)
                {
                    this.seriesPanel.scrollPanePosition = this.seriesPanel.scrollPanePosition + delta;
                }
            }
            catch (err:Error)
            {
                Logger.error("doMouseWheel, no scrollpane");
            }
            return;
        }// end function

        public function showSearchResult(param1:int = -1) : void
        {
            this.searchPanel.showSearchResult(param1);
            return;
        }// end function

        public function setPageArrowAvail(param1:Boolean, param2:Boolean) : void
        {
            if (this.pageGroup)
            {
                this.pageGroup.setPageArrowAvail(param1, param2);
            }
            return;
        }// end function

        public function get autoSearchIndex() : int
        {
            return this.searchPanel.autoSearchIndex;
        }// end function

        public function get searchKeyWord() : String
        {
            return this.searchPanel.searchKeyWord;
        }// end function

        public function set pageth(param1:int) : void
        {
            this._pageth = Math.max(0, param1);
            this._pageth = Math.min((this._pagesAllAvailable - 1), this._pageth);
            if (this._pageth)
            {
                this.setPageArrowAvail(true, true);
            }
            else
            {
                this.setPageArrowAvail(true, false);
            }
            if (this._pageth < (this._pagesAllAvailable - 1))
            {
                this.setPageArrowAvail(false, true);
            }
            else
            {
                this.setPageArrowAvail(false, false);
            }
            if (this.loaderInfoVo.bookmarkPage && this.loaderInfoVo.bookmarkPage > 1 && this.loaderInfoVo.bookmarkPage <= this._pagesAllAvailable && this.bmFirstTime)
            {
                this.addBookMarkBtn();
                this.bmFirstTime = false;
            }
            if (this._pageth == (this.loaderInfoVo.newmarkPage - 1))
            {
                this.toolButtonLayout.disableBtnByName("newmarkBtn");
            }
            else
            {
                this.toolButtonLayout.enableBtnByName("newmarkBtn");
            }
            return;
        }// end function

        public function get pageth() : int
        {
            return this._pageth;
        }// end function

    }
}
